home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 040a / randtune.zip / RANDTUNE.C next >
C/C++ Source or Header  |  1991-11-25  |  3KB  |  128 lines

  1.  
  2. /*////////////////////////////////////////////////////////////////////////////
  3. //                                                                          //
  4. //  Maximus Random Tune Selector                                            //
  5. //  Copyright 1991 by Peter G. Zion.  All rights reserved.                  //
  6. //  Source code may be freely distributed as long as copyright message is   //
  7. //  not removed from this file or the compiled executable.                  //
  8. //                                                                          //
  9. ////////////////////////////////////////////////////////////////////////////*/
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16.  
  17. #define NLEN    80
  18. #define MAXLLEN 512
  19.  
  20. char tunes_bbs[NLEN] = "Tunes.BBS", events_bbs[NLEN] = "Events00.BBS";
  21. int num_tunes, tune_nums[256];
  22.  
  23. void HelpMe(void) {
  24.  
  25.   printf( "Usage:\n        RANDLANG [<events_bbs> [<tunes_bbs>]]\n\n<events_bbs> is the name of the events file for Maximus\n(optional, defaults to EVENTS00.BBS).  <tunes_bbs> is the name of the\nMaximus \"tunes\" file (optional, defaults to TUNES.BBS).\n" );
  26.   exit(0);
  27.  
  28. }
  29.  
  30. void GetTunes(void) {
  31.  
  32.   FILE *fp;
  33.   char line[MAXLLEN];
  34.  
  35.   if( NULL == (fp = fopen( tunes_bbs, "rt" )) ) {
  36.     printf( "Error: tunes file \"%s\" not found\n\n", tunes_bbs );
  37.     HelpMe();
  38.   }
  39.  
  40.   printf( "Getting tunes from \"%s\"...", tunes_bbs );
  41.   num_tunes = 0;
  42.  
  43.   while( !feof( fp ) ) {
  44.     fgets( line, MAXLLEN, fp );
  45.     if( strnicmp( line, "* Yell", 6 ) == 0 )
  46.       tune_nums[num_tunes++] = atoi( &line[6] );
  47.   }
  48.  
  49.   fclose( fp );
  50.  
  51.   if( num_tunes == 0 ) {
  52.     printf( "\nError: No tunes found in \"%s\"\n\n", tunes_bbs );
  53.     HelpMe();
  54.   }
  55.  
  56.   printf( "\n" );
  57.  
  58. }
  59.  
  60. void WriteNewTune(void) {
  61.  
  62.   FILE *in, *out;
  63.   char tmpname[NLEN];
  64.   char line[MAXLLEN], *p;
  65.  
  66.   tmpnam( tmpname );
  67.  
  68.   if( NULL == (in = fopen( events_bbs, "rt" )) ) {
  69.     printf( "Error: events file \"%s\" not found\n", events_bbs );
  70.     HelpMe();
  71.   }
  72.  
  73.   if( NULL == (out = fopen( tmpname, "wt" )) ) {
  74.     printf( "Error: unable to create temporary file \"%s\"\n", tmpname );
  75.     HelpMe();
  76.   }
  77.  
  78.   printf( "Writing new random tune..." );
  79.   srand( (unsigned)time(NULL) );
  80.  
  81.   while( !feof(in) ) {
  82.     fgets( line, MAXLLEN, in );
  83.     if( strnicmp( line, "Event", 5 ) == 0 ) {
  84.       if( NULL != (p = strchr( line, 'y' )) || NULL != (p = strchr( line, 'Y' )) ) {
  85.         if( NULL != (p = strchr( p, '/' )) ) {
  86.           ++p;
  87.           if( NULL != (p = strchr( p, '/' )) ) {
  88.             sprintf( p, "/%d\n", tune_nums[rand()%num_tunes] );
  89.           }
  90.         }
  91.       }
  92.     }
  93.     fputs( line, out );
  94.   }
  95.  
  96.   fclose(in);
  97.   fclose(out);
  98.  
  99.   remove( events_bbs );
  100.   rename( tmpname, events_bbs );
  101.  
  102.   printf( "\n" );
  103.  
  104. }
  105.  
  106. void main( int argc, char *argv[] ) {
  107.  
  108.   printf( "\nRandTune -- Random Tune Selector for Maximus 2.00\nCopyright 1991 by Peter G. Zion.  All rights reserved.\n\n" );
  109.  
  110.   if( argc > 3 )
  111.     HelpMe();
  112.  
  113.   if( argc > 1 ) {
  114.     strncpy( events_bbs, argv[1], NLEN );
  115.     if( argc > 2 )
  116.       strncpy( tunes_bbs, argv[2], NLEN );
  117.   }
  118.  
  119.   GetTunes();
  120.  
  121.   WriteNewTune();
  122.  
  123.   printf( "Done!\n" );
  124.  
  125. }
  126.  
  127.  
  128.